home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / MenuShortcut.java < prev    next >
Text File  |  1998-09-22  |  3KB  |  107 lines

  1. /*
  2.  * @(#)MenuShortcut.java    1.10 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.event.KeyEvent;
  17.  
  18. /**
  19.  * A class which represents a keyboard accelerator for a MenuItem.
  20.  *
  21.  * @version 1.10, 07/01/98
  22.  * @author Thomas Ball
  23.  */
  24. public class MenuShortcut implements java.io.Serializable 
  25. {
  26.  
  27.     int key;
  28.     boolean usesShift;
  29.  
  30.     /*
  31.      * JDK 1.1 serialVersionUID 
  32.      */
  33.      private static final long serialVersionUID = 143448358473180225L;
  34.  
  35.     /**
  36.      * Constructs a new MenuShortcut for the specified key.
  37.      * @param key the raw keycode for this MenuShortcut, as would be returned
  38.      * in the keyCode field of a KeyEvent if this key were pressed.
  39.      **/
  40.     public MenuShortcut(int key) {
  41.         this(key, false);
  42.     }
  43.  
  44.     /**
  45.      * Constructs a new MenuShortcut for the specified key.
  46.      * @param key the raw keycode for this MenuShortcut, as would be returned
  47.      * in the keyCode field of a KeyEvent if this key were pressed.
  48.      * @param useShiftModifier indicates whether this MenuShortcut is invoked
  49.      * with the SHIFT key down.
  50.      **/
  51.     public MenuShortcut(int key, boolean useShiftModifier) {
  52.         // Convenience conversion for programmers who confuse key posts with
  53.         // ASCII characters -- do not internationalize!  They *should* be
  54.         // using KeyEvent virtual keys, such as VK_A.
  55.         if (key >= 'a' && key <= 'z') {
  56.             key = (int)Character.toUpperCase((char)key);
  57.         }
  58.         this.key = key;
  59.         this.usesShift = useShiftModifier;
  60.     }
  61.  
  62.     /**
  63.      * Return the raw keycode of this MenuShortcut.
  64.      */
  65.     public int getKey() {
  66.         return key;
  67.     }
  68.  
  69.     /**
  70.      * Return whether this MenuShortcut must be invoked using the SHIFT key.
  71.      */
  72.     public boolean usesShiftModifier() {
  73.         return usesShift;
  74.     }
  75.  
  76.     /**
  77.      * Returns whether this MenuShortcut is the same as another:
  78.      * equality is defined to mean that both MenuShortcuts use the same key
  79.      * and both either use or don't use the SHIFT key.
  80.      * @param s the MenuShortcut to compare with this.
  81.      */
  82.     public boolean equals(MenuShortcut s) {
  83.     return (s != null && (s.getKey() == key) && 
  84.                 (s.usesShiftModifier() == usesShift));
  85.     }
  86.  
  87.     /**
  88.      * Returns an internationalized description of the MenuShortcut.
  89.      */
  90.     public String toString() {
  91.         int modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  92.         if (usesShiftModifier()) {
  93.             modifiers |= Event.SHIFT_MASK;
  94.         }
  95.     return KeyEvent.getKeyModifiersText(modifiers) + "+" + 
  96.                KeyEvent.getKeyText(key);
  97.     }
  98.  
  99.     protected String paramString() {
  100.         String str = "key=" + key;
  101.     if (usesShiftModifier()) {
  102.         str += ",usesShiftModifier";
  103.     }
  104.     return str;
  105.     }
  106. }
  107.